unit SpecName;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB;

type
  TfrmSpecName = class(TForm)
    lblSpecNameIntro: TLabel;
    btnSpecCreate: TButton;
    btnCancel: TButton;
    edtSpecName: TEdit;
    lblSpecExistWarn: TLabel;
    procedure edtSpecNameExit(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnSpecCreateClick(Sender: TObject);
    procedure SpecNameCheck(SpecName: string);
    procedure FormCreate(Sender: TObject);
    procedure CopyConv(Dataset:Tdataset);
    procedure edtSpecNameKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmSpecName: TfrmSpecName;
  lvProceed: boolean;

implementation

uses main;

{$R *.dfm}

procedure TfrmSpecName.btnSpecCreateClick(Sender: TObject);
begin
  SpecNameCheck(edtSpecName.Text);

  if lvProceed then
  begin
    frmMain.tblSpecs.Open;

    if main.lvSpecNew then
    begin
      frmMain.tblSpecs.Append;
      frmMain.tblSpecsSpecName.AsString  := edtSpecName.Text;
      frmMain.tblSpecsCreated.AsDateTime := Now;
      frmMain.tblSpecs.Post;
    end;

    if main.lvSpecRename then
    begin
      frmMain.tblSpecs.FindKey([main.lvSelected]);
      frmMain.tblSpecs.Edit;
      frmMain.tblSpecsSpecName.AsString := edtSpecName.Text;
    end;

    if main.lvSpecCopy then
    begin
      frmMain.tblSpecs.FindKey([main.lvSelected]);
      CopyConv(frmMain.tblSpecs);
    end;

    self.Close;
  end;
end;

procedure TfrmSpecName.edtSpecNameExit(Sender: TObject);
begin
  SpecNameCheck(edtSpecName.Text);
end;

procedure TfrmSpecName.edtSpecNameKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_RETURN then
    btnSpecCreate.Click;
end;

procedure TfrmSpecName.btnCancelClick(Sender: TObject);
begin
  if not lvProceed then
  begin
    frmMain.pgCtrl.ActivePageIndex := 2;
  end;

  self.Close;
end;

procedure TfrmSpecName.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  main.lvSpecNew    := False;
  main.lvSpecCopy   := False;
  main.lvSpecEdit   := False;
  main.lvSpecRename := False;

  if not lvProceed then
    frmMain.pgCtrl.ActivePageIndex := 0;
end;

procedure TfrmSpecName.FormCreate(Sender: TObject);
begin
  if main.lvSpecRename then
  begin
    self.Caption := 'Specification Rename';
    lblSpecNameIntro.Caption := 'New name of specification:';
    btnSpecCreate.Caption := 'Rename';
    edtSpecName.Text := frmMain.tblSpecsSpecName.AsString;
  end;

  if main.lvSpecCopy then
  begin
    self.Caption := 'Specification Dupe';
    lblSpecNameIntro.Caption := 'Name of duplicated specification:';
    btnSpecCreate.Caption := 'Duplicate';
    edtSpecName.Text := frmMain.tblSpecsSpecName.AsString;
  end;
end;

{$REGION 'Custom Functions/Procedures'}
procedure TfrmSpecName.SpecNameCheck(SpecName: string);
begin
  frmMain.tblSpecs.Open;

  if frmMain.tblSpecs.FindKey([SpecName]) then
  begin
    edtSpecName.Font.Color := clRed;
    lblSpecExistWarn.Visible := True;
    lvProceed := False;
  end
  else
  begin
    edtSpecName.Font.Color := clWindowText;
    lblSpecExistWarn.Visible := False;
    lvProceed := True;
  end;

  frmMain.tblSpecs.Close;
end;

procedure TfrmSpecName.CopyConv(Dataset:Tdataset);
var
  lvFields : Variant;
  lvFieldCount : integer;
begin
  lvFields := VarArrayCreate([0,Dataset.FieldCount-1],VarVariant);

  for lvFieldCount := 0 to (DataSet.FieldCount-1) do
  begin
    lvFields[lvFieldCount] := DataSet.Fields[lvFieldCount].Value;
  end;

  DataSet.Append;
  for lvFieldCount := 0 to (DataSet.FieldCount-1) do
  begin
    DataSet.Fields[lvFieldCount].Value := lvFields[lvFieldCount];
  end;
  DataSet.Post;
end;
{$ENDREGION}

end.
